"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : autocommit
Case Name   : 使用set方式设置autocommit，当前会话有效
Create At   : 2023/01/16
@zou_jialiang0501162244
Description :
    1、创建兼容B库
    2、查询autocommit的值，默认on
    3、设置autocommit开启和关闭，查询
    4、新建连接查询autocommit的值
    5、清理环境
Expect      :
    1、成功
    2、查询成功，on
    3、执行成功
    4、查询成功，on
    5、成功
History     :
"""

import os
import unittest

from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class TestAutocommit(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_autocommit_0002'

    def test_autocommit(self):
        text1 = '-----step1:创建兼容B数据库;expect:成功-----'
        self.log.info(text1)
        create_db = f"drop database if exists {self.db_name};" \
                    f"create database {self.db_name} dbcompatibility 'B';"
        createdb_res = self.pri_sh.execut_db_sql(create_db)
        self.log.info(createdb_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, createdb_res,
                      f"创建兼容B库失败")

        text2 = '-----step2:查询autocommit的值;expect:查询成功，on-----'
        self.log.info(text2)
        default_res = self.pri_sh.execut_db_sql('show autocommit;',
                                                dbname=self.db_name)
        self.log.info(default_res)
        self.assertIn('on', default_res, f'执行失败: {text2}')

        text3 = '-----step3:设置autocommit开启和关闭，查询;expect:执行成功-----'
        self.log.info(text3)
        sql = f"set autocommit = 0; show autocommit;" \
              f"set autocommit = 1; show autocommit;" \
              f"set autocommit to off; show autocommit;"
        sql_res = self.pri_sh.execut_db_sql(sql, dbname=self.db_name)
        self.log.info(sql_res)
        self.assertEqual(sql_res.count('SET'), 3, f'执行失败: {text3}')
        self.assertEqual(sql_res.count('on'), 1, f'执行失败: {text3}')
        self.assertEqual(sql_res.splitlines()[3].strip(), 'off',
                         f'执行失败: {text3}')
        self.assertEqual(sql_res.splitlines()[-2].strip(), 'off',
                         f'执行失败: {text3}')

        text4 = '-----step4:新建连接查询autocommit的值;expect:on-----'
        self.log.info(text4)
        session_res = self.pri_sh.execut_db_sql('show autocommit;',
                                                dbname=self.db_name)
        self.log.info(session_res)
        self.assertIn('on', session_res, f'执行失败: {text4}')

    def tearDown(self):
        text5 = '-----step5:清理环境; expect:成功-----'
        self.log.info(text5)
        create_db_res = self.pri_sh.execut_db_sql(
            f"drop database if exists {self.db_name};")
        self.log.info(create_db_res)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, create_db_res,
                      f'删库失败: {text5}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
